﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Security;
 
namespace Gmis.Public
{
    public class CookieFunction
    {
        #region ------------读写Cookie值---------------

        #region 写Cookie值
        public static void SetCookie(string strName, string strValue)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
            if (cookie == null)
            {
                cookie = new HttpCookie(strName);
            }
            cookie.Path = "/";
            cookie.Value = HttpUtility.UrlEncode(strValue);
            HttpContext.Current.Response.AppendCookie(cookie);
           
        }
        #endregion

        #region  写cookie值+1重载函数 加时间
        public static void SetCookie(string strName, string strValue, int expires)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
            if (cookie == null)
            {
                cookie = new HttpCookie(strName);
            }
            cookie.Path = "/";
            cookie.Value = strValue;
            cookie.Expires = DateTime.Now.AddMinutes(expires);
            HttpContext.Current.Response.AppendCookie(cookie);

        }
        #endregion

        #region 写cookie值+1重载函数 加子键/值
        public static void SetCookie(string strName, string strKeyName, String strKeyValue)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
            if (cookie == null)
            {
                cookie = new HttpCookie(strName);
            }
            cookie.Values[strKeyName] = strKeyValue;
            HttpContext.Current.Response.AppendCookie(cookie);

        }
        #endregion

        #region //写cookie值+2重载函数 加子键/值 过期时间分钟计算
        public static void SetCookie(string strName, string strKeyName, String strKeyValue, int expires)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
            if (cookie == null)
            {
                cookie = new HttpCookie(strName);
            }
            cookie.Values[strKeyName] = strKeyValue;
            cookie.Expires = DateTime.Now.AddMinutes(expires);
            //这里过期时间能影响到所有该集合下所有子键的过期时间
            HttpContext.Current.Response.AppendCookie(cookie);
        }
        #endregion

        #region //读cookie值
        public static string GetCookie(string strName)
        {
            if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[strName] != null)
            {                
                return HttpUtility.UrlDecode(HttpContext.Current.Request.Cookies[strName].Value.ToString());
            }

            return string.Empty;
        }
        #endregion

        #region 读cookie值+1重载,读取Cookie子键值
        public static string GetCookie(string strName, string strKeyName)
        {
            if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[strName] != null && HttpContext.Current.Request.Cookies[strName].Values[strKeyName] != null)
            {
                return HttpContext.Current.Request.Cookies[strName].Values[strKeyName].ToString();
            }

            return string.Empty;
        }
        #endregion

        public static void CreateCustomCookie(string username,bool isPersistent)
        {

            //ticket中没有设置用户数据，用户的角色信息不再放到此处，用的时候直接用 Roles.IsUserInRole来判断
            //创建身份验证票据
            FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, username, DateTime.Now, DateTime.Now.AddMinutes(40), isPersistent, "");
            //加密票据
            string encryptedTicket = FormsAuthentication.Encrypt(ticket);
            //创建新的cookie
            HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName);
            //将加密后的票据信息加入cookie
            cookie.Value = encryptedTicket;
            cookie.HttpOnly = false;
            if (isPersistent)
            {
                cookie.Expires = DateTime.Now.AddMonths(1);
            }
            else
            {
                //cookie.Expires = ticket.Expiration;//不设置即为临时cookie，关闭浏览器即失效
            }
            //把cookie加入到响应流中
            HttpContext.Current.Response.Cookies.Add(cookie);
            //把cookie发送到客户端

            //创建用户标识
            FormsIdentity identity = new FormsIdentity(ticket);

            //创建用户的主体信息
            System.Security.Principal.GenericPrincipal user =
            new System.Security.Principal.GenericPrincipal(identity, null);
           HttpContext.Current.User = user;


        }
        #endregion

    }
}
